package com.mzba.easy.weibo.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;

/**
 * @author 06peng
 *
 */
public class UserEntryTable {

	public String TABLE_NAME = "users_db";
	public static final String _id = "_id";
	public static final String screen_name = "screen_name";
	public static final String name = "name";
	public static final String id = "id";
	public static final String province = "province";
	public static final String city = "city";
	public static final String location = "location";
	public static final String description = "description";
	public static final String url = "url";
	public static final String profile_image_url = "profile_image_url";
	public static final String domain = "domain";
	public static final String gender = "gender";
	public static final String followers_count = "followers_count";
	public static final String friends_count = "friends_count";
	public static final String statuses_count = "statuses_count";
	public static final String favourites_count = "favourites_count";
	public static final String created_at = "created_at";
	public static final String following = "following";
	public static final String follow_me = "follow_me";
	public static final String type = "type";

	
	private DBManagerImpl db = null;
	
	public UserEntryTable() {
		if (db == null) {
			db = DBManager.get();
		}
		if (!db.isTableExits(db.getConnection(), TABLE_NAME)) {
			createTable();
		}
	}
	
	public void createTable() {
		String createTableSql = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME
				+ "(_id integer primary key autoincrement, " 
				+ name + " VARCHAR," 
				+ created_at + "  VARCHAR," 
				+ id + " VARCHAR,"
				+ location + " VARCHAR,"
				+ description + " VARCHAR," 
				+ province + " Integer," 
				+ city + " Integer," 
				+ url + " VARCHAR," 
				+ profile_image_url + " VARCHAR,"
				+ domain + " VARCHAR," 
				+ gender + " VARCHAR,"
				+ followers_count + " Integer,"
				+ friends_count + " Integer," 
				+ statuses_count + " Integer, "
				+ statuses_count + " integer," 
				+ screen_name + " VARCHAR, "
				+ following + " Integer,"
				+ type + " Integer,"
				+ follow_me + " Integer)";
		db.creatTable(db.getConnection(), createTableSql, TABLE_NAME);
		
	}
	
	public void save(List<UserEntry> entries, int usertype) {
		try {
			List<ContentValues> arraylist = new ArrayList<ContentValues>();
			db.delete(db.getConnection(), TABLE_NAME, null, null);
			for (int i = 0;i < 20;i++) {
				UserEntry entry = entries.get(i);
				ContentValues values = new ContentValues();
				values.put(name, entry.getName());
				values.put(created_at, entry.getCreated_at());
				values.put(id, entry.getId());
				values.put(province, entry.getProvince());
				values.put(city, entry.getCity());
				values.put(location, entry.getLocation());
				values.put(description, entry.getDescription());
				values.put(url, entry.getUrl());
				values.put(profile_image_url, entry.getProfile_image_url());
				values.put(domain, entry.getDomain());
				values.put(gender, entry.getGender());
				values.put(followers_count, entry.getFollowers_count());
				values.put(friends_count, entry.getFriends_count());
				values.put(statuses_count, entry.getStatuses_count());
				values.put(favourites_count, entry.getFavourites_count());
				values.put(following, entry.isFollowing() ? 1 : 0);
				values.put(follow_me, entry.isFollow_me() ? 1 : 0);
				values.put(screen_name, entry.getScreen_name());
				values.put(type, usertype);
				arraylist.add(values);
			}
			db.saveAll(db.getConnection(), TABLE_NAME, arraylist);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public List<UserEntry> getUsersByType(int type) {
		Cursor cursor = db.find(db.getConnection(), "select * from " + TABLE_NAME + " where type=?", new String[]{String.valueOf(type)});
		if (cursor == null)
			return null;
		List<UserEntry> entries = new ArrayList<UserEntry>();
		try {
			while (cursor.moveToNext()) {
				UserEntry entry = new UserEntry();
				entry.setName(cursor.getString(cursor.getColumnIndex(name)));
				entry.setCreated_at(cursor.getString(cursor.getColumnIndex(created_at)));
				entry.setId(cursor.getString(cursor.getColumnIndex(id)));
				entry.setProvince(cursor.getInt(cursor.getColumnIndex(province)));
				entry.setCity(cursor.getInt(cursor.getColumnIndex(city)));
				entry.setFollowing(cursor.getInt(cursor.getColumnIndex(following)) == 1 ? true : false);
				entry.setFollow_me(cursor.getInt(cursor.getColumnIndex(follow_me)) == 1 ? true : false);
				entry.setProfile_image_url(cursor.getString(cursor.getColumnIndex(profile_image_url)));
				entry.setLocation(cursor.getString(cursor.getColumnIndex(location)));
				entry.setDescription(cursor.getString(cursor.getColumnIndex(description)));
				entry.setUrl(cursor.getString(cursor.getColumnIndex(url)));
				entry.setDomain(cursor.getString(cursor.getColumnIndex(domain)));
				entry.setGender(cursor.getString(cursor.getColumnIndex(gender)));
				entry.setStatuses_count(cursor.getInt(cursor.getColumnIndex(statuses_count)));
				entry.setFollowers_count(cursor.getInt(cursor.getColumnIndex(followers_count)));
				entry.setFriends_count(cursor.getInt(cursor.getColumnIndex(friends_count)));
				entry.setFavourites_count(cursor.getInt(cursor.getColumnIndex(favourites_count)));
				entry.setScreen_name(cursor.getString(cursor.getColumnIndex(screen_name)));
				entries.add(entry);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (cursor != null && !cursor.isClosed()) {
				cursor.close();
			}
		}
		return entries;
	}
	
	public void clearTable() {
		db.delete(db.getConnection(), TABLE_NAME, null, null);
	}
}
